﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraTab;
using DevExpress.XtraTab.ViewInfo;
using MicropleSoft.Controls.DialogBox;
using MicropleSoft.WmsApp.Base.Menu;
using MicropleSoft.WmsApp.Base.Window;
using MicropleSoft.WmsApp.Library.ServiceFacade;

namespace MicropleSoft.WmsApp.BaseDataManage
{
    public partial class FormBaseDataMain : BaseForm
    {
        private List<ItemClass> _itemClassList = null;
        private List<ItemClassKind> _itemClassKindList = null;
        public FormBaseDataMain()
        {
            InitializeComponent();
            LocdTreeList();
            RegisterTreeListNodeRelateForm();
        }

        private void LocdTreeList()
        {
            _itemClassList = ServerProxy.Query<ItemClass>("GetItemClassList", " and 1=1 ");
            _itemClassKindList = ServerProxy.Query<ItemClassKind>("GetItemClassKindList", " and 1=1");
            TreeListBinder.BindingTreeListStaticData(this.xTreeListMain, _itemClassList, _itemClassKindList);
        }

        private void RegisterTreeListNodeRelateForm()
        {
            this.xTreeListMain.DoubleClick += (o, e) =>
            {
                if (this.xTreeListMain.FocusedNode != null
                    && !this.xTreeListMain.FocusedNode.HasChildren
                    && this.xTreeListMain.FocusedNode.Tag != null
                    && this.xTreeListMain.FocusedNode.Tag is ItemClass)
                {
                    var model = this.xTreeListMain.FocusedNode.Tag as ItemClass;
                    LoadForm(model);
                }
            };
        }

        private void LoadForm(ItemClass itemClass)
        {
            var tabPages =  this.xXtraTabControlMain.TabPages.Where(x => x.Text == itemClass.ClassName);
            if (tabPages.FirstOrDefault() == null)
            {
                var xtraTabPage = new XtraTabPage();
                xtraTabPage.Tag = itemClass.ClassNumber;
                xtraTabPage.Name = "xtraTabPage";
                xtraTabPage.ShowCloseButton = DefaultBoolean.True;
                xtraTabPage.Text = itemClass.ClassName;
                
                var xBaseDataChild = new XBaseDataChild();
                xtraTabPage.Controls.Add(xBaseDataChild);
                xBaseDataChild.Dock = DockStyle.Fill;

                this.xXtraTabControlMain.TabPages.AddRange(new DevExpress.XtraTab.XtraTabPage[] {xtraTabPage});
                xBaseDataChild.LoadTreeItem(itemClass.ClassNumber);
            }
            else
            {
                this.xXtraTabControlMain.SelectedTabPage = tabPages.FirstOrDefault();
            }
        }

        private void XXtraTabControlMainCloseButtonClick(object sender, EventArgs e)
        {
            var closePageButtonEventArgs = (ClosePageButtonEventArgs) e;
            var tabPage = (XtraTabPage) closePageButtonEventArgs.Page;

            this.xXtraTabControlMain.TabPages.Remove(tabPage);
            if (tabPage.TabIndex > 0)
            {
                this.xXtraTabControlMain.SelectedTabPageIndex = tabPage.TabIndex - 1;
            }
        }

    }
}
